package com.jtpay.sdk.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.jtpay.sdk.R;
import com.jtpay.sdk.util.JftLogUtil;

/**
 * Created by Administrator on 2016/6/18 0018.
 */

public class RecycleViewDivider extends RecyclerView.ItemDecoration {
    private Drawable mDrawable;

    public RecycleViewDivider(Context context) {
        int version = android.os.Build.VERSION.SDK_INT;
        JftLogUtil.log("RecycleViewDivider version = " +version);
        if (version < 21){
            mDrawable = context.getResources().getDrawable(R.drawable.line_divider);
        } else {
            mDrawable = context.getDrawable(R.drawable.line_divider);
        }
    }

    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        JftLogUtil.log("onDrawOver");
        int left = parent.getLeft();
        int right = parent.getWidth() - parent.getPaddingRight();
        int count = parent.getChildCount();
        JftLogUtil.log("onDrawOver count = " + count);
        for (int i = 0; i < count; i++) {
            View child = parent.getChildAt(i);
            int bottom = child.getBottom();
            int height = mDrawable.getIntrinsicHeight();
            int top = bottom - height;
            mDrawable.setBounds(left, top, right, bottom);
            mDrawable.draw(c);
        }
    }
}
